package com.msxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msxt.annotation.IgnoreAuth;
import com.msxt.dto.SystemintroEntity;
import com.msxt.result.R;
import com.msxt.service.SystemintroService;
import com.msxt.utils.MPUtil;
import com.msxt.utils.PageUtils;
import com.msxt.view.SystemintroView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/systemintro")
public class SystemintroController {
    @Autowired
    private SystemintroService systemintroService;

    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params,
                  SystemintroEntity systemintro,
                  HttpServletRequest request) {
        QueryWrapper<SystemintroEntity> wrapper = new QueryWrapper<>();

        // 添加默认排序条件（防止空排序）
        if (!params.containsKey("sort")) {
            params.put("sort", "id");
            params.put("order", "desc");
        }

        PageUtils page = systemintroService.queryPage(params,
                MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, systemintro), params), params));
        return R.ok().put("data", page);
    }

    @IgnoreAuth
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params,
                  SystemintroEntity systemintro,
                  HttpServletRequest request) {
        QueryWrapper<SystemintroEntity> wrapper = new QueryWrapper<>();
        PageUtils page = systemintroService.queryPage(params,
                MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(wrapper, systemintro), params), params));
        return R.ok().put("data", page);
    }

    @GetMapping("/lists")
    public R list(SystemintroEntity systemintro) {
        QueryWrapper<SystemintroEntity> wrapper = new QueryWrapper<>();
        wrapper.allEq(MPUtil.allEQMapPre(systemintro, "systemintro"));
        return R.ok().put("data", systemintroService.list(wrapper));
    }

    @GetMapping("/query")
    public R query(SystemintroEntity systemintro) {
        QueryWrapper<SystemintroEntity> wrapper = new QueryWrapper<>();
        wrapper.allEq(MPUtil.allEQMapPre(systemintro, "systemintro"));
        SystemintroView systemintroView = systemintroService.selectView(wrapper);
        return R.ok("查询关于我们成功").put("data", systemintroView);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SystemintroEntity systemintro = systemintroService.getById(id);
        return R.ok().put("data", systemintro);
    }

    @IgnoreAuth
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        SystemintroEntity systemintro = systemintroService.getById(id);
        return R.ok().put("data", systemintro);
    }

    @PostMapping("/save")
    public R save(@RequestBody SystemintroEntity systemintro,
                  HttpServletRequest request) {
        systemintro.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        systemintroService.save(systemintro);
        return R.ok();
    }

    @PostMapping("/add")
    public R add(@RequestBody SystemintroEntity systemintro,
                 HttpServletRequest request) {
        systemintro.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        systemintroService.save(systemintro);
        return R.ok();
    }

    @PostMapping("/update")
    @Transactional
    public R update(@RequestBody SystemintroEntity systemintro,
                    HttpServletRequest request) {
        systemintroService.updateById(systemintro);
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        systemintroService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName,
                         HttpServletRequest request,
                         @PathVariable("type") String type,
                         @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        if("2".equals(type)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if(map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if(map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        QueryWrapper<SystemintroEntity> wrapper = new QueryWrapper<>();
        if(map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if(map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }

        long count = systemintroService.count(wrapper);
        return R.ok().put("count", count);
    }
}